using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace PlatformShoot
{
    public class MainPanel : PlatformShootGameController
    {
        private Text mScoreTex;

        private void Start()
        {
            //Debug.Log(TimeHelper.GetNetDateTime()) ;
            mScoreTex = transform.Find("ScoreTex").GetComponent<Text>();

            transform.Find("SettingBtn").GetComponent<Button>()
                .onClick.AddListener(OnOpenSetting);

            this.GetModel<IGameModel>().Score.RegisterWithInitValue(OnScoreChanged)
                .UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private void OnOpenSetting()
        {
            ResHelper.AsyncLoad<GameObject>("Item/SettingPanel", o=> 
            {
                o.transform.SetParent(GameObject.Find("Canvas").transform);
                (o.transform as RectTransform).anchoredPosition = Vector2.zero;
            });
        }
        private void OnScoreChanged(int score)
        {
            mScoreTex.text = score.ToString();
        }

        public void UpdateScoreTex(int score)
        {
            int temp = int.Parse(mScoreTex.text);
            mScoreTex.text = (temp + score).ToString();
        }
    }
}